﻿using System;
using System.Data;
using System.Linq;
using AccessControl.Common;
using AccessControl.Common.Enumerations;
using AccessControl.DataAcess;
using AccessControl.Utility.Tools;


namespace AccessControl.Business
{
    public class EventLogBusiness
    {

        public static bool ReportPersonAccessResult(string pkPerson, ref AccessControlDS.tblEventLogDataTable eventlogTable, int fkWorkflow, EventResultConstants result, string reason, string eventTypeName)
        {
            var newPerson = false;

            AccessControlDS.tblEventLogRow eventLogRow;
            AccessControlDS.tblEventLogRow previousEventlogRow=null;
            if(eventlogTable.Rows.Count>0)  previousEventlogRow= (AccessControlDS.tblEventLogRow)eventlogTable.Rows[0];
            if (previousEventlogRow == null || pkPerson != previousEventlogRow.fkPerson){
                eventlogTable.Clear();
                eventLogRow = eventlogTable.NewtblEventLogRow();
                newPerson = true;
            }else
                eventLogRow = previousEventlogRow;

            eventLogRow.DateRegistered = DateTime.Now;
            eventLogRow.fkPerson = pkPerson;
            eventLogRow.fkWorkflow = fkWorkflow;
            var guestDT=GuestBusiness.SelectByFilter(String.Format("fkPerson='{0}'",pkPerson));
            if (guestDT != null) eventLogRow.fkGuest = guestDT.Single().pkGuest;
            eventLogRow.Reason = reason;
            eventLogRow.Result = (byte)result;
            eventLogRow.EventType=eventTypeName;

            if (newPerson){
                EventLogAccess.Insert(eventLogRow);
                eventlogTable.AddtblEventLogRow(eventLogRow);
            }
            else
                EventLogAccess.Update(eventLogRow);
            
            return newPerson;
        }

        public static AccessControlDS.tblEventLogDataTable SelectAll()
        {
            return EventLogAccess.SelectAll();
        }

        public static AccessControlDS.tblEventLogDataTable SelectByFilter(string filter)
        {
            return EventLogAccess.SelectByFilter(filter);
        }

        public static DataTable SelectByFilterForReport(string filter, int groupType, DateTime startDate, DateTime endDate,bool selectLast)
        {
            return selectLast ? EventLogAccess.SelectByFilterForReportDistinct(filter, groupType, startDate, endDate) : EventLogAccess.SelectByFilterForReport(filter,  groupType, startDate, endDate);
        }

        public static void Insert(AccessControlDS.tblEventLogRow row)
        {
            EventLogAccess.Insert(row);
        }

        public static void Update(AccessControlDS.tblEventLogRow row)
        {
            EventLogAccess.Update(row);
        }

        public static void Delete(AccessControlDS.tblEventLogRow row)
        {
            EventLogAccess.Delete(row);
        }

        public static bool  HasRecord(String pkPerson)
        {
            return EventLogAccess.HasRecord(pkPerson);
        }

        public static bool HasTwinEvent(Int64 pkEventLog)
        {
            return EventLogAccess.HasTwinEvent(pkEventLog);
        }
    }
}
